package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.modules.sys.model.SysFile;
import com.thm.tfast.modules.sys.service.SysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 文件上传记录表 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-27
 */
@RestController
@Api(tags = "系统：文件管理")
@RequestMapping("/sysFile")
public class SysFileController {

    @Resource
    private SysFileService sysFileService;

    @ApiOperation(value = "上传文件")
    @PostMapping("/upload")
    public ViewResult<Long> upload(@RequestParam("file") MultipartFile file,
                                   @RequestParam(value = "type", defaultValue = "0",required = false) Integer type) {
        return ViewResult.success(sysFileService.upload(file,type));
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/download/{fileId}")
    public void download(@PathVariable Long fileId, HttpServletResponse response) {
        sysFileService.download(fileId, response);
    }

    @ApiOperation("分页获取文件记录信息")
    @GetMapping(value = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：文件名称"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysFile>> page(@RequestParam(value = "keyword", required = false) String keyword,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysFile> page = sysFileService.page(keyword, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定文件记录信息")
    @GetMapping("/{fileId}")
    public ViewResult<SysFile> getUserById(@PathVariable Long fileId) {
        SysFile sysFile = sysFileService.getById(fileId);
        return ViewResult.success(sysFile);
    }

    @ApiOperation("删除文件记录信息")
    @DeleteMapping("/delete/{fileId}")
    public ViewResult<String> delete(@PathVariable Long fileId) {
        boolean success = sysFileService.delete(fileId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }
}

